{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.4\n"
     ]
    }
   ],
   "source": [
    "num1 = 2.0\n",
    "num2 = 5\n",
    "operator = \"/\"\n",
    "ans = {\n",
    "    '+': lambda x: num1 + num2,\n",
    "    '-': lambda x: num1 - num2,\n",
    "    '*': lambda x: num1 * num2,\n",
    "    '/': lambda x: num1 / num2\n",
    "}[operator](x=operator)\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calc_num(num1, num2, op):\n",
    "    num2 = float(num2)  # R\n",
    "    num1 = float(num1)  # L\n",
    "    operator = op\n",
    "    result = {\n",
    "        '+': lambda x: num1 + num2,\n",
    "        '-': lambda x: num1 - num2,\n",
    "        '*': lambda x: num1 * num2,\n",
    "        '/': lambda x: num1 / num2\n",
    "    }[operator](x=operator)\n",
    "    return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:59:23.463768Z",
     "start_time": "2022-05-06T07:59:23.456180Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6-5+5.*9+6-7/9\n",
      "num: ['6', '5', '5.', '9', '6', '7', '9']\n",
      "ops: ['-', '+', '*', '+', '-', '/']\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "# a 的优先级是否高于 b\n",
    "def priority(a, b):\n",
    "    if (a == '+' or a == '-'):\n",
    "        return False\n",
    "    elif (a == '*' or a == '/'):\n",
    "        return (b == '+' or b == '-')\n",
    "    return False\n",
    "\n",
    "# 计算\n",
    "def calc_num(num2, num1, op):\n",
    "    num1 = float(num1)  # L\n",
    "    num2 = float(num2)  # R\n",
    "    operator = op\n",
    "    result = {\n",
    "        '+': lambda x: num1 + num2,\n",
    "        '-': lambda x: num1 - num2,\n",
    "        '*': lambda x: num1 * num2,\n",
    "        '/': lambda x: num1 / num2\n",
    "    }[operator](x=operator)\n",
    "    return result\n",
    "\n",
    "op_list = []\n",
    "nums = []\n",
    "ops = []\n",
    "calc_str = \"6-5+5.*9+6-7/9\"  # test\n",
    "\n",
    "# 健壮性\n",
    "if calc_str[0] == \"-\":\n",
    "    # calc_str[0] == \".\" or\n",
    "    # ui.lb_calc.setText(\"请在小数点前加数字\")\n",
    "    calc_str = \"0\" + calc_str\n",
    "\n",
    "# 存放数字\n",
    "opera_nums = re.split(\"\\+|-|\\*|/\", calc_str)\n",
    "# 存放操作符\n",
    "operators = re.sub(\"[\\d]\", \"\", calc_str)\n",
    "operators = re.sub(\"\\.\", \"\", operators)\n",
    "for i in range(len(operators)):\n",
    "    op_list.append(operators[i])\n",
    "\n",
    "print(calc_str)\n",
    "print(\"num:\",opera_nums)\n",
    "print(\"ops:\",op_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-06T07:59:29.673910Z",
     "start_time": "2022-05-06T07:59:29.661607Z"
    }
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1753891426.py, line 14)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  Input \u001b[0;32mIn [2]\u001b[0;36m\u001b[0m\n\u001b[0;31m    ops.append(op_list[j]) if not j == len(op_list) - 1\u001b[0m\n\u001b[0m                                                       ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# 循环计算\n",
    "for j in range(len(opera_nums)):\n",
    "    nums.append(opera_nums[j])\n",
    "    # 空栈直接入栈\n",
    "    if not len(ops):\n",
    "        ops.append(op_list[j])\n",
    "    else:\n",
    "        # 若不是高优先级则符号出栈\n",
    "        if not priority(op_list[j], ops[-1]):\n",
    "            result = calc_num(nums.pop(), nums.pop(), ops.pop())\n",
    "            nums.append(result)  # 将新计算出来的结果入栈\n",
    "            ops.append(op_list[j])\n",
    "        else:\n",
    "            ops.append(op_list[j]) if not j == len(op_list) - 1\n",
    "        print(j, result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-94.0"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "calc_num(6,100,\"-\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8c8977ec7ec5e51ff4245d97f068bebf3ad1b19029c7d38ef0d3393da5a038c2"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
